/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2024-03-13 10:57:26
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-03-21 14:40:48
 * @FilePath: \dyh-vue3-demo\src\store\modules\menu.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineStore } from 'pinia'
import { store } from '@/store/index'
import { addRoutes } from '@/router/routerHelper'

export interface MenuItem {
  id: number | string;
  path?: string; // 跳转路径
  name?: string; // 可以进行路由跳转（只有页面存在该属性）
  title: string; // 菜单名称
  keepAlive?: boolean; // 是否缓存该页面（只有页面存在该属性）
  icon?: string; // 菜单图标名称
  children?: MenuItem[];
  component?: string; // 组件路径，动态添加路由时使用
}

interface MenuInfo {
  menuList: MenuItem[];
  routers: null;
  btnList: string[];
}

export const useMenuStore = defineStore('menuInfo', {
  state: (): MenuInfo => {
    return {
      menuList: [],
      routers: null,
      btnList: ['demo:list', 'demo:add', 'demo:edit', 'demo:delete']
    }
  },
  getters: {}, // 可以将计算属性放在 getters 中使用，以便其在状态变化时产生
  actions: {
    // 一般动态生成菜单在此进行
    setMenuList () {
      // return new Promise((resolve, reject) => {
      //   resolve(this.menuList)
      // })
      this.menuList = [
        { id: 1, path: '/home', name: 'Home', title: '首页', keepAlive: true, icon: 'home.png', children: [], component: '/home/home' },
        {
          id: 2,
          path: '/demo',
          title: '案例',
          icon: 'demo.png',
          children: [
            { id: '2-1', path: '/commonScreen', name: 'CommonScreen', title: '公共组件使用', children: [], component: '/demo/common-component' },
            { id: '2-2', path: '/routerParams', name: 'RouterParams', title: '路由传参案例', children: [], component: '/demo/router-params' },
            { id: '2-3', path: '/form-screen', name: 'FormScreen', title: 'Form表单筛选项', children: [], component: '/demo/form-screen' },
            { id: '2-4', path: '/my-table', name: 'MyTable', title: 'vueHooks列表', children: [], component: '/demo/my-table' }
          ]
        },
        {
          id: 3,
          // path: '/system',
          title: '权限管理',
          icon: 'setting.png',
          children: [
            { id: '3-1', path: '/user-manage', name: 'UserManage', title: '用户管理', children: [], component: '/system/user-manage' },
            { id: '3-2', path: '/menu', name: 'Menu', title: '菜单管理', children: [], component: '/system/menu-manage' }
          ]
        }
      ]
      addRoutes(this.menuList)
    }
  }
})

export function useMenuStoreWithOut () {
  return useMenuStore(store)
}
